//
//  PlayDetailManager.swift
//  TimMusic
//
//  Created by nkl on 2024/10/12.
//

import Foundation
import TSVideoKit
import UIKit

class PlayDetailManager {
    static let shared: PlayDetailManager = .init()

    var playerVc: TSVideoPlayController {
        if let rootVc = UIApplication.shared.delegate?.window??.rootViewController,
           let tabVc = rootVc as? TMTabbarViewController {
            return tabVc.playDetailVc.playerVc
        }
        return TSVideoOperator.shared.playerController
    }
    
    func nextMode(){
        playerVc.viewModel.loopMode = playerVc.viewModel.loopMode.nextMode()
    }
    
    func pauseOrPlay(){
        playerVc.pauseOrPlay()
    }
    
    func playNext(){
        playerVc.playNext()
    }
    
    func playLast(){
        playerVc.playLast()
    }
    

    func playVideo(video: TSVideo, list: [TSVideo], scene: TSPlayScene, autoPlay: Bool = true) {
        showPlayDetailVc()
        PlayDetailManager.shared.playerVc.playVideo(video: video, list: list, scene: scene)
    }

    func playOnlineVideo(video: VideoOnlineModel, recommendDaras: [VideoOnlineModel], scene: TSPlayScene, autoPlay: Bool = true) {
        showPlayDetailVc()
        PlayDetailManager.shared.playerVc.playOnlineVideo(video: video, recommendDaras: recommendDaras, scene: scene, autoPlay: autoPlay)
    }

    func showPlayDetailVc() {
        if let rootVc = UIApplication.shared.delegate?.window??.rootViewController,
           let tabVc = rootVc as? TMTabbarViewController {
            tabVc.showPlayDetail()
        }
    }

    func hiddePlayDetailVc() {
        if let rootVc = UIApplication.shared.delegate?.window??.rootViewController,
           let tabVc = rootVc as? TMTabbarViewController {
            tabVc.hidePlayDetail()
        }
    }
}
